package com.ruoyi.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.business.entity.YytHomeSubject;
import com.ruoyi.common.business.service.YytHomeSubjectService;
import com.ruoyi.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ruoyi.common.business.entity.YytHomeOneSubject;
import com.ruoyi.common.business.service.YytHomeOneSubjectService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
* <p>
    *  前端控制器
    * </p>
*
* @author MrPing
* @since 2022-04-01
*/
@RestController
@RequestMapping("/homeType")
public class YytHomeOneSubjectController {

    @Autowired
    private YytHomeOneSubjectService yytHomeOneSubjectService;

    @Autowired
    private YytHomeSubjectService yytHomeSubjectService;

    @GetMapping("/getHomeTypeData")
    public ResponseResult getHomeTypeData(){
        List<YytHomeOneSubject> list = yytHomeOneSubjectService.list(new LambdaQueryWrapper<YytHomeOneSubject>()
                .eq(YytHomeOneSubject::getStatus, 0).orderByAsc(YytHomeOneSubject::getSort));
        List<List<YytHomeOneSubject>> result = new ArrayList<>(list.size() / 10 + 1);
        List<YytHomeOneSubject> itemList = new ArrayList<>(10);
        // 组装成列表嵌套列表 内列表10个一组
        for (YytHomeOneSubject item : list) {
            if (itemList.size() >= 10) {
                result.add(itemList);
                itemList = new ArrayList<>(10);
            }
            itemList.add(item);
        }
        result.add(itemList);
        return ResponseResult.success(result);
    }

    @GetMapping("/getHomeSubjectList")
    public ResponseResult getHomeSubjectList(@RequestParam("hosId") String hosId){
        QueryWrapper<YytHomeSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("hos_id", hosId)
                .eq("status", 0)
                .orderByAsc("sort");

        List<YytHomeSubject> list = yytHomeSubjectService.list(wrapper);
        List<List<YytHomeSubject>> result = new ArrayList<>(list.size() / 10 + 1);
        List<YytHomeSubject> itemList = new ArrayList<>(10);
        // 组装成列表嵌套列表 内列表10个一组
        for (YytHomeSubject item : list) {
            if (itemList.size() >= 10) {
                result.add(itemList);
                itemList = new ArrayList<>(10);
            }
            itemList.add(item);
        }
        result.add(itemList);
        return ResponseResult.success(result);

    }

    /**
     * 获取专题数据
     * @return
     */
    @GetMapping("/getHomeSubject")
    public ResponseResult getHomeSubject(){
        return ResponseResult.success(yytHomeSubjectService.list());
    }

}
